﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace fluentvaly
{
    public class ValidatorConfiguration
    {
        public ValidatorConfiguration()
        {
            Validators = new List<BaseValidationSetUp>();
        }

        protected IList<BaseValidationSetUp> Validators { get; private set; }

        protected void Register(BaseValidationSetUp setUp)
        {
            Validators.Add(setUp);
        }

        protected InvariantValidator<T> DefineInvariantFor<T>(bool throwsException)
        {
            var invariantValidator = new InvariantValidator<T>(throwsException);

            Register(invariantValidator);

            return invariantValidator;
        }

        protected InvariantValidator<T> DefineInvariantFor<T>(bool throwsException, string defaultInvalidationMessage, InvalidationReasons defaultInvalidationReason)
        {
            var invariantValidator = new InvariantValidator<T>(throwsException, defaultInvalidationMessage, defaultInvalidationReason);

            Register(invariantValidator);

            return invariantValidator;
        }

        public bool IsDefinedFor(Type type)
        {
            return (from validator in Validators
                    where validator.GetDefinedType() == type
                    select validator).Any();
        }

        public IEnumerable<IValidationStatement> GetAllForType(Type type)
        {
            //todo: ensure that exception is not propagated to outside code
            return (from setup in Validators
                    where setup.GetDefinedType() == type
                    select setup.ValidationStatements).Single();
        }

        public IEnumerable<Type> GetAllSetUpTypes()
        {
            return from validator in Validators
                   select validator.GetDefinedType();
        }
    }
}
